package com.sky.sd.system.config;

import com.sky.sd.common.constant.AppConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

/**
 * <br/>功能:
 * <br/>版本: 1.0
 * <br/>开发人员: 弓振
 * <br/>创建日期: 2020/3/10 8:42
 * <br/>修改日期: 2020/3/10 8:42
 * <br/>修改列表:
 */
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Slf4j
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.httpBasic()
                .and()
            .exceptionHandling().authenticationEntryPoint(
                (request, response, authException) -> {
                    log.info("==> ResourceServerConfig 扩展点 ");
                }).accessDeniedHandler((request, response, accessDeniedException) -> {
                    log.info("==> ResourceServerConfig 扩展点 ");
                })
                .and()
            .authorizeRequests().antMatchers(StringUtils.join(AppConstant.CLIENT_INTERNAL_CALL,"/**")).permitAll()
                .anyRequest().permitAll();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationEntryPoint((request, response, authException) -> {
            log.info("==> ResourceServerConfig 扩展点 ");
        });
    }
}
